#include "library.h"



const char *insert_into_user_table=  "insert into user(username,password,sex)values('%s','%s','%s');";

const char *create_user_table=  "create table if not exists user(id integer primary key autoincrement,"
                                "username text unique not null,"
                                "password text not null,"
                                "sex text not null,"    //性别，M-男，W-女
                                "power integer not null default 0);";   //权限，0-普通用户，1-管理员（如果希望添加黑名单，可以设置-1为黑名单）
const char *create_book_table=  "create table if not exists book(id integer primary key autoincrement,"
                                "bookname text not null,"
                                "author text not null,"
                                "publisher text not null,"  //出版社
                                "category text not null,"   //类型
                                "years integer not null,"   //出版年份
                                "num integer not null,"     //剩余数量（如为0，则不可借阅）
                                "price integer not null,"   //价格，逾期按照价格赔偿
                                "status integer not null,"//状态，可借阅或不可借阅（个人感觉有剩余数量可以去掉这个）
                                "borrowerId INTEGER DEFAULT -1,"
                                "borrowDate INTEGER DEFAULT 0,"
                                "returnDate INTEGER DEFAULT 0);";
const char *create_record_table="create table if not exists record (id integer primary key autoincrement,"
                                "username integer not null,"  //用户id，非用户名
                                "bookname integer not null,"  //书id，非书名
                                "borrow_date integer not null default 0,"//借出时间(默认借书期限为30天)
                                "return_date integer not null default 0);";//归还时间
                                


// 初始化系统
void initSystem() {
    int rc = sqlite3_open(DATABASE_FILE, &db);//DATABASE_FILE="library.db"
    if (rc != SQLITE_OK) {
        fprintf(stderr, "无法打开数据库: %s\n", sqlite3_errmsg(db));
        sqlite3_close(db);
        exit(1);
    }
   

    createTables();
  

    
    sqlite3_exec(db,"insert into user(username,password,sex,power)values('root','root123456','M',1);",NULL,0,NULL);

    
}

// 创建数据库表
void createTables() {
    int res=0;
  

    res=sqlite3_exec(db,create_user_table,NULL,0,NULL);
    if(res!=SQLITE_OK)
    {
        printf("%d\n",res);
        fprintf(stderr,"create user table error:%s\n",sqlite3_errmsg(db));
        return ;
    }

    res=sqlite3_exec(db,create_book_table,NULL,0,NULL);
    if(res!=SQLITE_OK)
    {
        fprintf(stderr,"create message table error:%s\n",sqlite3_errmsg(db));
        return ;
    }

    res=sqlite3_exec(db,create_record_table,NULL,0,NULL);
    if(res!=SQLITE_OK)
    {
        fprintf(stderr,"create message table error:%s\n",sqlite3_errmsg(db));
        return ;
    }


     res = sqlite3_exec(db, create_review_table, NULL, 0, NULL);
    if(res != SQLITE_OK) {
        fprintf(stderr, "create review table error:%s\n", sqlite3_errmsg(db));
        return;
    }


    

   
}


int InsertUserTable(const char *username,const char* password,const char *sex)
{
    char sql[LEN],sql_2[LEN];
    sqlite3_stmt *stmt;
    snprintf(sql,sizeof(sql),insert_into_user_table,username,password,sex);
    int res=sqlite3_exec(db,sql,NULL,0,NULL);
    if(res!=SQLITE_OK)
    {
        fprintf(stderr,"insert user error:%s\n",sqlite3_errmsg(db));
        return -1;
    }
    snprintf(sql_2,sizeof(sql_2),"select id from user where username = '%s';",username);
    res=sqlite3_prepare_v2(db,sql_2,-1,&stmt,NULL);
    if(res!=SQLITE_OK)
    {
        sqlite3_finalize(stmt);
        return -1;
    }
    if(sqlite3_step(stmt)==SQLITE_ROW)
    {
        res=sqlite3_column_int(stmt,0);
    }
    else return -1;
    
    return res;
}